﻿namespace LuckyShell;

public static class Api
{
    public static string ObjToValueType(object o)
    {
        return o switch
        {
            string => "String",
            decimal => "Number",
            bool => "Bool",
            _ => "Object"
        };
    }

    public static string Help => "";

    public static string ShowFile(FileInfo info, string showString = "")
    {
        return showString switch
        {
            "" => $"Name : {info.Name}{Environment.NewLine}" +
                  $"FullName : {info.FullName}{Environment.NewLine}" +
                  $"Extension : {info.Extension}{Environment.NewLine}" +
                  $"Size : {FileSizeString(info.Length)}{Environment.NewLine}" +
                  $"Directory Name : {info.DirectoryName}{Environment.NewLine}" +
                  $"-----------------------{Environment.NewLine}" +
                  $"Last Write Time : {info.LastWriteTime:F}{Environment.NewLine}" +
                  $"Creation Time : {info.CreationTime:F}{Environment.NewLine}" +
                  $"-----------------------{Environment.NewLine}" +
                  $"Is ReadOnly : {info.IsReadOnly}{Environment.NewLine}" +
                  $"Attributes : {info.Attributes}",
            "Name" or "name" => info.Name,
            "FullName" or "full" => info.FullName,
            "Extension" or "ext" => info.Extension,
            "Size" or "size" => FileSizeString(info.Length),
            "Directory" or "dir" => info.DirectoryName!,
            "LastWriteTime" or "lwt" => info.LastWriteTime.ToString("F"),
            "CreationTime" or "ct" => info.CreationTime.ToString("F"),
            "ReadOnly" or "readonly" => info.IsReadOnly.ToString(),
            "Attributes" or "ass" => info.Attributes.ToString(),
            _ => ""
        };
    }

    public static string ShowDir(DirectoryInfo info, string showString = "")
    {
        return showString switch
        {
            "" => $"Name : {info.Name}{Environment.NewLine}" +
                  $"FullName : {info.FullName}{Environment.NewLine}" +
                  $"Directory Name : {info.Parent?.Name}{Environment.NewLine}" +
                  $"-----------------------{Environment.NewLine}" +
                  $"Last Write Time : {info.LastWriteTime:F}{Environment.NewLine}" +
                  $"Creation Time : {info.CreationTime:F}{Environment.NewLine}" +
                  $"-----------------------{Environment.NewLine}" +
                  $"Attributes : {info.Attributes}",
            "Name" or "name" => info.Name,
            "FullName" or "full" => info.FullName,
            "Directory" or "dir" => info.Parent?.Name!,
            "LastWriteTime" or "lwt" => info.LastWriteTime.ToString("F"),
            "CreationTime" or "ct" => info.CreationTime.ToString("F"),
            "Attributes" or "ass" => info.Attributes.ToString(),
            _ => ""
        };
    }

    private static string FileSizeString(this long l)
    {
        var unit = new[] { "B", "KB", "MB", "GB", "TB" };
        var i = 0;
        while (true)
        {
            if (l / 1024 < 1024)
                break;
            l /= 1024;
            i++;
        }

        return $"{l}{unit[i]}";
    }

    public static string Test(string test)
    {
        return test switch
        {
            "set" => """
                     a = 1
                     """,
            "interpolate" => """
                             a = "list-sdks"
                             -> "dotnet --@a"
                             """,
            "print" => """
                       a = 1
                       Print --a;
                       """,
            "show" => """
                      Show --"C:\\";
                      """,
            "delete" => """
                        a = 1
                        Delete --a;
                        Print --a;
                        """,
            "for" => """
                     for a = 0,a<2,a = a+1
                        Print --a;
                     """,
            "while" => """
                       a = 1
                       while a == 2
                          a = 2
                          Print --a;
                       """,
            "pass" => """
                      a = 1
                      pass
                      """,
            "app" => """
                     -> "dotnet help"
                     """,
            "appValue" => """
                          a = -> "dotnet --list-sdks"
                          """,

            _ => ""
        };
    }

    public static string[] TestString
        => new[]
        {
            "set", "interpolate", "print", "show", "delete",
            "for", "while", "pass", "app", "appValue"
        };
}